package protocol.JT808_2019;

import protocol.JT905_2014.IHandle905_2014JT;
import protocol.JT905_2014.JT_905_2014Cmd;

import java.util.HashMap;

public class Handle808_2019JT80 implements IHandle808_2019JT {
    @Override
    public String handle(JT808_2019Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x01:
                return handle8001(cmd);
            case 0x03:
                return handle8003(cmd);
            case 0x04:
                return handle8004(cmd);
            default:
                return "808-2019:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle8003(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:服务器补传分包请求");
        builder.append("\r\n");
        builder.append("原始消息流水号："+cmd.readInt(2));
        builder.append("\r\n");
        int size  = cmd.readInt(2);
        builder.append("重传包总数："+size);
        builder.append("\r\n");

        for (int i = 0; i < size; i++) {
            builder.append("    补传ID："+cmd.readInt(2));
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle8004(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:终端查询服务器时间，服务器应答");
        builder.append("\r\n");
        builder.append("服务器时间："+cmd.readBCD(6));
        builder.append("\r\n");
        return builder.toString();
    }

    private String handle8001(JT808_2019Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:中心通用应答");
        builder.append("\r\n");
        builder.append("应答流水号："+cmd.readBCD(2));
        builder.append("\r\n");
        builder.append("应答ID："+cmd.readBCD(2));
        builder.append("\r\n");

        HashMap<Integer,String> resMap = new HashMap<>();
        resMap.put(0,"成功/确认");
        resMap.put(1,"失败");
        resMap.put(2,"消息有误");
        resMap.put(3,"不支持");
        resMap.put(4,"报警处理确认");

        int res = cmd.readInt(1);
        builder.append("结果（"+res+"):"+resMap.getOrDefault(res,"未定义"));
        builder.append("\r\n");

        return builder.toString();
    }

}
